/*
 * 文件名：UserInfoController.java	 
 * 时     间：下午4:20:10
 * 作     者：liyf       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.cz073586.technology.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cz073586.common.contants.CommonContans;
import com.cz073586.common.controller.BaseController;
import com.cz073586.common.entity.pojo.PageInfo;
import com.cz073586.common.shiro.utils.ShiroUtil;
import com.cz073586.technology.model.UserInfo;
import com.cz073586.technology.service.face.UserInfoService;
import com.cz073586.tools.privilege.ResourceAndOperate;
import com.cz073586.tools.util.Tools;
import com.cz073586.tools.util.UUIDGenerateUtil;

/**
 * @ClassName: UserInfoController
 * @Description: 这里用一句话描述这个类的作用
 * @author liyf
 * @date 2015年1月20日 下午4:20:10
 */
@RestController
@RequestMapping(value = "/userManager", method = RequestMethod.POST)
@ResourceAndOperate(
		moduleID = "com.technology.userManager", 
		moduleName = "用户管理", 
		resouceDesc = "用户管理",	
		parentModuleID = "com.technology", 
		parentModuleName = "昱显科技项目"
		)
public class UserInfoController extends BaseController
{
	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 
	 * @方法名称: queryServicesByPage
	 * @描述: 查询用户列表
	 *
	 * @参数:
	 * @param page
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryUserByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.queryUserByPage", 
			moduleName = "查询", 
			resouceDesc = "",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap queryUserListByPage(@RequestBody PageInfo<UserInfo> page)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			userInfoService.queryByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryUserList.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.queryUserList", 
			moduleName = "查询", 
			resouceDesc = "",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap queryUserList(@RequestBody UserInfo user)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA, userInfoService.queryUserList(user));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：
	 * @Title: login 
	 * @param @param username
	 * @param @param password
	 * @param @return
	 * @return ModelMap
	 * @user Administrator
	 * 2015年1月25日
	 * @throws
	 */
	@RequestMapping(value = "/login.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.login", 
			moduleName = "用户登录", 
			resouceDesc = "-1：不存在该用户0：密码错误success：登录成功，返回用户表所有字段的信息(包括用户权限和积分)",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap login(String username,String password)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			UserInfo user = new UserInfo();
			if(Tools.isEmpty(username))
			{
				resultMap.put(CommonContans.STRUTS_DATA, "username is Required");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_ERROR);
				return resultMap;
			}
			if(Tools.isEmpty(password))
			{
				resultMap.put(CommonContans.STRUTS_DATA, "password is Required");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_ERROR);
				return resultMap;
			}
			user.setUserName(username);
			user = userInfoService.queryUserInfo(user);
			if(null==user)
			{
				resultMap.put(CommonContans.STRUTS_DATA, "-1");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_ERROR);
				return resultMap;
			}
			user.setPassword(password);
			user = userInfoService.queryUserInfo(user);
			if(null==user)
			{
				resultMap.put(CommonContans.STRUTS_DATA, "0");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_ERROR);
				return resultMap;
			}
			user = userInfoService.queryUserInfo(user);
			
			user = userInfoService.queryUserInfo(user);
			resultMap.put(CommonContans.STRUTS_DATA, user);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryUserById
	 * @描述: 通过用户ID查询
	 *
	 * @参数:
	 * @param userId
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryUserById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.queryUserById", 
			moduleName = "查询单一对象", 
			resouceDesc = "",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap queryUserById(String userId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					userInfoService.selectByPrimaryKey(userId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加用户信息，uuid由代码写入
	 * 
	 * @Title: insertUser
	 * @param @param user
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/insertUser.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.insertUser", 
			moduleName = "添加", 
			resouceDesc = "0：用户名已经存在1：注册成功",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap insertUser(@RequestBody UserInfo user)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			UserInfo userInfo = new UserInfo();
			userInfo.setUserName(user.getUserName());
			userInfo = userInfoService.queryUserInfo(userInfo);
			if(null!=userInfo)
			{
				resultMap.put(CommonContans.STRUTS_DATA,"0");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_SUCCESS);
				return resultMap;
			}
			user.setUuid(UUIDGenerateUtil.generate());
			resultMap.put(CommonContans.STRUTS_DATA,
					userInfoService.insert(user)+"");
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据user主机修改用户信息
	 * 
	 * @Title: updateUser
	 * @param @param user
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/updateUser.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.updateUser", 
			moduleName = "更新", 
			resouceDesc = "",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap updateUser(@RequestBody UserInfo user)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					userInfoService.updateByPrimaryKeySelective(user));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键删除用户
	 * 
	 * @Title: deleteUser
	 * @param @param userId
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/deleteUser.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.userManager.deleteUser", 
			moduleName = "删除", 
			resouceDesc = "",
			parentModuleID = "com.technology.userManager", 
			parentModuleName = "用户管理"
			)
	public ModelMap deleteUser(@RequestBody UserInfo user)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if(StringUtils.isBlank(user.getUuid()))
			{
				resultMap.put(CommonContans.STRUTS_DATA,"user uuid is empty..");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_ERROR);
				return resultMap;
			}
			resultMap.put(CommonContans.STRUTS_DATA,
					userInfoService.deleteByPrimaryKey(user.getUuid()));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
}
